<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="病历号" prop="CaseNumber">
        <el-input v-model="form.CaseNumber" :disabled="true"/>
      </el-form-item>
      <el-form-item label="挂号id" prop="RegisterId">
        <el-input v-model="form.RegisterId" placeholder="请输入挂号id" :disabled="true"/>
      </el-form-item>
      <el-form-item label="主诉" prop="MedicalReadme">
        <el-input v-model="form.MedicalReadme" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="现病史" prop="MedicalPresent">
        <el-input v-model="form.MedicalPresent" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="现病治疗情况" prop="PresentTreat">
        <el-input v-model="form.PresentTreat" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="既往史" prop="MedicalHistory">
        <el-input v-model="form.MedicalHistory" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="过敏史" prop="MedicalAllergy">
        <el-input v-model="form.MedicalAllergy" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="体格检查" prop="MedicalPhysique">
        <el-input v-model="form.MedicalPhysique" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="检查建议" prop="TestOrder">
        <el-input v-model="form.TestOrder" placeholder="请输入检查建议"/>
        <el-button class="furtherMove" @click="handleCheckApply">检查申请</el-button>
      </el-form-item>
      <el-form-item label="检查结果" prop="MedicalTested">
        <el-input v-model="form.MedicalTested" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="诊断结果" prop="MedicalDiagnosis">
        <el-input v-model="form.MedicalDiagnosis" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
      <el-form-item label="处理意见" prop="MedicalHandling">
        <el-input v-model="form.MedicalHandling" placeholder="请输入内容" type="textarea"/>
        <el-button class="furtherMove" @click="handlePrescription">开具处方</el-button>
      </el-form-item>
      <el-form-item label="病历状态" prop="CaseState">
        <el-select v-model="form.CaseState" placeholder="请选择病历状态">
          <el-option
            v-for="dict in dict.type.case_state"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm(true)">诊 毕</el-button>
      <el-button @click="submitForm(false)">提 交</el-button>
    </div>
  </div>
</template>

<script>
import {addMedicalRecord, getByRegisterId, updateMedicalRecord} from "@/api/system/MedicalRecord";
import {addPrescription} from "@/api/system/Prescription"
import getNowFormatDate from "@/utils/getDate";
import {getByMedicalRecordId} from "@/api/system/CheckDetailed";

export default {
  name: "MedicalRecordInfo",
  dicts: ['case_state'],
  data() {
    return {
      form: {},
      rules: {
        CaseNumber: [
          {required: true, message: "病历号不能为空", trigger: "blur"}
        ],
        RegisterId: [
          {required: true, message: "挂号id不能为空", trigger: "blur"}
        ],
        MedicalTested: [
          {required: true, message: "检查结果不能为空", trigger: "blur"}
        ],
        MedicalDiagnosis: [
          {required: true, message: "诊断结果不能为空", trigger: "blur"}
        ],
        CaseState: [
          {required: true, message: "病历状态不能为空", trigger: "change"}
        ],
      }
    };
  },
  async mounted() {
    await getByRegisterId(this.$route.query.RegisterId).then(res => {
      this.form = res;
      this.form.CaseState = 1;
      this.form.RegisterId = this.$route.query.RegisterId;
      this.form.CaseNumber = this.$route.query.CaseNumber;
    });
    await getByMedicalRecordId(this.form.Id).then((res) => {
      this.form.MedicalTested = "";
      for (let resKey in res) {
        this.form.MedicalTested += `${res[resKey].Result}; `;
      }
    })
  },
  methods: {
    handleCheckApply() {
      this.$confirm("确定要申请检查吗？").then(async () => {
        if (this.form.MedicalTested === null) {
          this.form.MedicalTested = "暂无";
          this.form.MedicalDiagnosis = "暂无";
          await addMedicalRecord(this.form);
          await getByRegisterId(this.$route.query.RegisterId).then(res => {
            this.form.Id = res.Id;
          })
        }
        await this.$router.push({
          path: '/medicalRecord/checkApply',
          query: {
            RegisterId: this.form.RegisterId,
            MedicalId: this.form.Id
          }
        })
      })
    },
    handlePrescription() {
      this.$confirm("确定要开具处方吗？").then(() => {
        let prescription = {
          Id: null,
          MedicalId: this.form.Id,
          UserId: localStorage.getItem("userId"),
          PrescriptionName: "成药处方",
          PrescriptionState: 2,
          PrescriptionTime: getNowFormatDate(),
          DelMark: 1,
          RegistId: this.form.RegisterId
        }
        addPrescription(prescription).then((res) => {
          this.$router.push({
            path: '/medicalRecord/drugsApply',
            query: {
              RegisterId: this.form.RegisterId,
              PrescriptionId: res.data
            }
          })
        })
      })
    },
    submitForm(flag) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          flag === true ? this.form.CaseState = 3 : this.form.CaseState = 2;
          if (this.form.Id != null) {
            updateMedicalRecord(this.form).then(() => {
            });
          } else {
            addMedicalRecord(this.form).then(() => {
            });
          }
          this.$router.push({
            path: '/medicalRecord/service'
          })
          this.$modal.msgSuccess("提交成功");
        }
      });
    },
  }
};
</script>

<style scoped>
.furtherMove {
  margin-top: 10px;
}
</style>
